package com.sky.controller.user;


import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@Api(tags = "用户地址相关接口")
@RequestMapping("/user/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;


    //--1--新增地址  /user/addressBook
    @PostMapping()
    public Result add(@RequestBody AddressBook addressBook){
        addressBookService.add(addressBook);
        return Result.success();
    }


    //--2--查询当前登录用户的所有地址信息    /user/addressBook/list
    @GetMapping("/list")
    public Result<List<AddressBook>> selectList(){
        return Result.success(addressBookService.selectList());
    }


    //--3-- 查询默认地址    /user/addressBook/default
    @GetMapping("default")
    public Result<AddressBook> selectdefault(){
        return Result.success(addressBookService.selectdefault());
    }

    //--4--根据id修改地址  /user/addressBook
    @PutMapping()
    public Result updateByUserId(@RequestBody AddressBook addressBook){
        addressBookService.updateByUserId(addressBook);
        return Result.success();
    }

    //--5--根据id删除地址   /user/addressBook
    @DeleteMapping()
    public Result deleteById(Long id){
        addressBookService.deleteById(id);
        return Result.success();
    }

    //--6--根据id查询地址   /user/addressBook/{id}
    @GetMapping("/{id}")
    public Result<AddressBook> selectById(@PathVariable Long id){
        return Result.success(addressBookService.selectById(id));
    }


    //--7--设置默认地址  /user/addressBook/default
    @PutMapping("/default")
    public Result upDefault(@RequestBody Map map){
        log.info(",,,{}",map);   //{id=1}
        Long id=Long.valueOf(map.get("id").toString());
        addressBookService.upDefault(id);
        return Result.success();
    }



}
